# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import json
import time
from typing import List
from pydantic import TypeAdapter
from tortoise.queryset import Q
from hypertext import PagingResult
from exception import AppException
from common.utils.urls import UrlUtil
from apps.admin.schemas import diy_schema as schema
from common.models.dev import DevDiyModel
from common.models.attach import AttachCateModel


class DiyService:

    @classmethod
    async def detail(cls, scene: int) -> schema.DiyDetailVo:
        """
        装修详情

        Args:
            scene (str): 场景编号

        Returns:
            schema.DiyDetailVo

        Author:
            zero
        """
        detail = await DevDiyModel.filter(scene=scene).get()
        data = json.loads(detail.data or "{}")
        return schema.DiyDetailVo(
            id=detail.id,
            scene=detail.scene,
            name=detail.name,
            data=data
        )

    @classmethod
    async def save(cls, post: schema.DiySaveIn, admin_id: int):
        """
        装修详情

        Args:
            post (schema.DiySaveIn): 装修数据
            admin_id (int): 管理员ID

        Author:
            zero
        """
        detail = await DevDiyModel.filter(scene=post.scene).get()
        try:
            await DevDiyModel.filter(id=detail.id).update(
                admin_id=admin_id,
                data=json.dumps(post.data, ensure_ascii=False),
                update_time=int(time.time())
            )
        except Exception as e:
            raise AppException(str(e))
